package com.simple.datax.flow.preprocessor.recvprotocol;

import java.util.List;

import com.alibaba.dubbo.common.logger.Logger;
import com.alibaba.dubbo.common.logger.LoggerFactory;
import com.simple.datax.flow.constants.ProcessResultConstants;
import com.simple.datax.flow.constants.TaskActivityConstants;
import com.simple.datax.flow.dao.ibatis.model.FlowPreProcessor;
import com.simple.datax.flow.message.BaseEcMessage;
import com.simple.datax.flow.message.EcFlowMessage;
import com.simple.datax.flow.message.EcFlowRespMessage;
import com.simple.datax.flow.service.PreProcessorManager;
import com.simple.datax.flow.service.TaskManager;

/** 
 * @Title: TcsFlow201Protocol.java 
 * @Package com.simple.datax.flow.preprocessor.recvprotocol 
 * @Description: TODO(接收到QP回执协议实现) 
 * @author tonney 
 * @date 2013-1-25 上午10:18:33 
 * @version V1.0 
 */
public class TcsFlow201Protocol implements RecvProtocolIF{
	public Logger log = LoggerFactory.getLogger(this.getClass());
	private PreProcessorManager preProcessorManager;

	private TaskManager taskManager;

	public void setPreProcessorManager(PreProcessorManager preProcessorManager) {
		this.preProcessorManager = preProcessorManager;
	}
	
	public void setTaskManager(TaskManager taskManager) {
		this.taskManager = taskManager;
	}

	public int process(BaseEcMessage ecMessage,
			List<BaseEcMessage> ecMessageList) {
		int result = ProcessResultConstants.STATUS_SUCCESS;
		FlowPreProcessor preProcessor = null;
		try{
			EcFlowMessage ecFlowMessage = (EcFlowMessage)ecMessage;
			String msgType = ecFlowMessage.getFlowMessageHead().getMessageType();
			String activityNo = ecFlowMessage.getFlowMessageBody().getActivityIdList().get(0);
			String ecode = ecFlowMessage.getFlowMessageHead().getSenderId();
			String flowid = taskManager.getFlowidByTaskid(ecFlowMessage.getFlowMessageBody().getTaskId());
			if(flowid!=null){
				preProcessor = preProcessorManager.getFlowPreProcessor(ecode,msgType,activityNo,flowid);
				if(preProcessor!=null){  //有预处理器
					String toActivitys = preProcessor.getTransActivityNo();
					String[] activityArray = toActivitys.split(TaskActivityConstants.ACTIVITY_SEPRATOR);
					if(activityArray.length>0){
						EcFlowMessage retEcFlowMessage = null;
						for(String actNo:activityArray){
							retEcFlowMessage = ecFlowMessage.clone();
							retEcFlowMessage.getFlowMessageBody().getActivityIdList().clear();
							retEcFlowMessage.getFlowMessageBody().getActivityIdList().add(actNo);
							retEcFlowMessage.setValidActivityNo(false);
							retEcFlowMessage.getFlowMessageBody().setFlowId(flowid);
							ecMessageList.add(retEcFlowMessage);
						}
					}
				}
			}else{
				result = ProcessResultConstants.STATUS_RECV_PROCESS_EXCEPTION;
				log.error("TCSPreProcessorImpl::recvPreProcessor can not find flowid by taskid["+ecFlowMessage.getFlowMessageBody().getTaskId()+"]");
			}
		}catch(Exception e){
			log.error("TCSPreProcessorImpl::recvPreProcessor error=>"+e.toString());
		}
		return result;
	}
}
